/**
 * @class
 * @memberof w.parser
 * @param {*} message 消息
 * @param {*} callback 处理完成的回调
 */
function createSphere(message, callback) {
    let { radius,
      widthSegments,
      heightSegments,
      phiStart,
      phiLength,
      thetaStart,
      thetaLength } = message;
  
    const thetaEnd = Math.min(thetaStart + thetaLength, Math.PI);
    const vertex = { x: 0, y: 0, z: 0 };
    const normal = { x: 0, y: 0, z: 0 };
    const indices = [];
    const vertices = [];
    const normals = [];
    const uvs = [];
    let index = 0;
    const grid = [];
    for (let iy = 0; iy <= heightSegments; iy++) {
      const verticesRow = [];
  
      const v = iy / heightSegments;
  
      // special case for the poles
      let uOffset = 0;
  
      if (iy === 0 && thetaStart === 0) {
        uOffset = 0.5 / widthSegments;
      } else if (iy === heightSegments && thetaEnd === Math.PI) {
        uOffset = -0.5 / widthSegments;
      }
  
      for (let ix = 0; ix <= widthSegments; ix++) {
        const u = ix / widthSegments;
  
        // vertex
        vertex.x =
          -radius *
          Math.cos(phiStart + u * phiLength) *
          Math.sin(thetaStart + v * thetaLength);
        vertex.y = radius * Math.cos(thetaStart + v * thetaLength);
        vertex.z =
          radius *
          Math.sin(phiStart + u * phiLength) *
          Math.sin(thetaStart + v * thetaLength);
  
        vertices.push(vertex.x, vertex.y, vertex.z);
  
        // normal
        normal.x = vertex.x;
        normal.y = vertex.y;
        normal.z = vertex.z;
        const scalar = 1 / Math.sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z)
        normal.x *= scalar;
        normal.y *= scalar;
        normal.z *= scalar;
        normals.push(normal.x, normal.y, normal.z);
  
        // uv
        uvs.push(u + uOffset, 1 - v);
  
        verticesRow.push(index++);
      }
  
      grid.push(verticesRow);
    }
    // indices
    for (let iy = 0; iy < heightSegments; iy++) {
      for (let ix = 0; ix < widthSegments; ix++) {
        const a = grid[iy][ix + 1];
        const b = grid[iy][ix];
        const c = grid[iy + 1][ix];
        const d = grid[iy + 1][ix + 1];
  
        if (iy !== 0 || thetaStart > 0) indices.push(a, b, d);
        if (iy !== heightSegments - 1 || thetaEnd < Math.PI)
          indices.push(b, c, d);
      }
    }
    callback({
      data: { indices: indices, position: vertices, normal: normals, texCood0: uvs },
      transferlist: [indices.buffer, vertices.buffer, normals.buffer, uvs.buffer]
    });
  }
  
  export { createSphere }
  